<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>模块管理</title>
</head>
<jsp:include page="/common/meta.jsp"></jsp:include>
<jsp:include page="/common/resource.jsp"></jsp:include>
<!-- 公用方法-->
<script type="text/javascript" src="${base}/common/js/listPageMaster.js"></script>

<!-- 新增，编辑 ，删除 ，查看 js方法-->
<script type="text/javascript">
    /** 初始化菜单树*/
    $(function () {
        loadTree();
    });
    <!-- datagird默认初始化url-->
    gridDataUrl = "" ;//'${base}/system/module/ModuleController/queryPaged';
    /** 加载模块树*/
    function loadTree() {
        $('#moduleTree').tree({
            url: '${base}/system/module/ModuleController/createModuleTree?lvl=1',
            method: 'GET',
            animate: true,
            lines: true,
            dnd: true,
            onBeforeDrag: function (node) {
                if (node.id == -1) {
                    return false;
                }
            },
            onDrop: function (target, source) {

                moveModule($(this).tree("getNode", target), source);
            },
            onClick: function (node) {
                if (node.state == 'open') {
                    $(this).tree('collapse', node.target)
                } else {
                    loadList(node);
                    $(this).tree('expand', node.target)
                }
            },
            onLoadSuccess: function () {
                debugger;
                var tempNode = $(this).tree('getSelected');
                if (null == tempNode) {
                    $(this).tree("select", $(this).tree("getRoot").target);
                    tempNode = $(this).tree('getSelected');
                }
                $(this).tree('expand', tempNode.target);
                loadList(tempNode);
            },
            onBeforeExpand: function (node) {
                loadChildList(node);
            }
        });
    }
    getCurrenNode = function () {
        var node = $('#moduleTree').tree("getSelected");
        if (node == null) {
            node = $('#moduleTree').tree("getRoot");
        }
        return node;
    }
    /* 模块移动**/
    function moveModule(target, source) {
        doAjaxRequest("${base}/system/module/ModuleController/update", {
            "id": source.id,
            "up_id": target.id,
            "up_name": target.text
        }, refreshTree);
    }

    /** 刷新模块树*/
    function refreshTree() {
      var   tempNode = getCurrenNode();
        loadList(tempNode);
    }

    /**
     * 加载子节点
     */
    loadChildList = function (node) {
        var nodeID = '';
        nodeID = node.id;
        $('#moduleTree').tree('options').url = "${base}/system/module/ModuleController/createSubModuleTree?up_id=" + nodeID;
    }
    /**
     * 加载右侧列表
     */
    loadList = function (node) {
        var up_idValue = '';
        if (node == null) {
            node = $('#moduleTree').tree("getSelected");
            if (null == node) {
                node = $('#moduleTree').tree("getRoot");
                up_idValue = node.id
            }
        }else {
            up_idValue = node.id;
        }

        $('#tblGrid').datagrid({
            url: '${base}/system/module/ModuleController/queryPaged',
            queryParams: {
                code: $('#code').val(),
                name: $('#name').val(),
                up_id: up_idValue,
            }
        });
    } //查询
    function doSearch() {
        $('#tblGrid').datagrid({
            url: '${base}/system/module/ModuleController/queryPaged',
            queryParams: {
                code: $('#code').val(),
                name: $('#name').val(),
                up_id: getCurrenNode().id,
            }
        });


    }

    function reloadTree() {
        $('#moduleTree').tree("reload");
    }

    function updateTreeNode(data) {
        var node = $('#moduleTree').tree('find', data.id);
        $("#moduleTree").tree("update", {
            target: node.target,
            text: data.name
        });
    }

    /*** 节点树新增*/
    function addTreeNode(data) {
        /** 如果是一级模块。直接添加到跟节点上*/
        var  parent = null;
        if(data.up_id > 0 ) {
            parent = getCurrenNode().target;
        }
            $("#moduleTree").tree("append", {
                parent:parent,
                data: [{
                    id: data.id,
                    text: data.name,
                    attributes: data,
                    state: 'closed'
                }]
            });

        /** 刷新节树节点*/
        refreshTree();
    }

    //查看动作
    function show() {
        var row = $("#tblGrid").datagrid("getSelected");
        if (row == null) {
            top. $.messager.alert('提示', '请选择要查看的数据', 'info');
            return;
        }
        //$("#save").remove();
        var showUrl = "${base}/system/module/ModuleController/show/" + row.id;
        openEasyuiDialog("模块查看", showUrl, 640, 550);
    }

    //新增动作
    function add() {
        var node = getCurrenNode();
        top.$.messager.confirm('确认','将要为《'+node.text+"》添加子节点？",function(r){
            if (r){
                var addUrl = "${base}/system/module/ModuleController/goAddPage?id=" + node.id;
                openEasyuiDialog("模块新增", addUrl, 640, 550);
            }
        });

    }
    //编辑动作
    function createDefaultButton() {
        var row = $("#tblGrid").datagrid("getSelected");
        if (row == null) {
            top.$.messager.alert('提示', '请选择要创建缺省按钮的数据', 'info');
            return;
        }
        top.$.messager.confirm('确认','确认要创建缺省按钮吗？',function(r){
            if (r){
                $.ajax({
                    url : "${base}/system/module/ModuleController/createDefaultButton",
                    data : JSON.stringify(row),
                    type : "POST",
                    dataType : "JSON",
                    cache : false,
                    contentType:"application/json; charset=utf-8",
                    beforeSend : function(xhr) {
                        /** 开启遮罩层次*/
                        $.easyui.loading({ msg: "请稍候...",topMost :true });
                    },
                    complete : function(xhr, ts) {
                        /** 关闭遮罩层*/
                        $.easyui.loaded({topMost :true});
                    },
                    success : function(data) {
                        var flag = data.flag;
                        if (flag) { //验证成功
                            top.$.messager.alert('提示', "创建成功", 'info', function() {
                                refreshTree();
                            });
                        } else { //验证失败
                            var msg = data.msg;
                            top.$.messager.alert('创建失败失败', msg, 'error');
                        }
                    }
                });
            }
        });

    }
    //编辑动作
    function edit() {
        var row = $("#tblGrid").datagrid("getSelected");
        if (row == null) {
            top.$.messager.alert('提示', '请选择要编辑的数据', 'info');
            return;
        }
        var editUrl = "${base}/system/module/ModuleController/edit/" + row.id;
        openEasyuiDialog("模块编辑", editUrl, 640, 550);
    }

    //删除动作
    function deleteRow() {
        var deleteUrl = "${base}/system/module/ModuleController/delete";
        var row = $("#tblGrid").datagrid("getSelected");
        if (!row) {
            top.$.messager.alert('提示', '请选择要删除的数据', 'info');
            return;
        }
        top.$.messager.confirm('提示', '确定要删除吗？', function (r) {
            if (r) {
                doDeleteRow(deleteUrl, {
                    "id": row.id
                });
            }
            loadTree();
        });
    }

</script>
<body>
<div id="layout" class="easyui-layout" style="width: 100%; height: 100%; background: #fff;">
    <!-- 查询条件 开始 -->
    <jsp:include page="vo/conditionVO.jsp"></jsp:include>
    <!-- 模块树 -->
    <jsp:include page="vo/tree.jsp"></jsp:include>
    <!-- 查询条件 结束 -->
    <jsp:include page="page/listComponent.jsp"></jsp:include>
</div>
</body>
</html>
